package com.fg.common.constant;

import com.fg.common.enums.OrderTypeEnum;
import com.google.gson.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 全局常量类
 * @author lv
 * @version 1.0.0
 */
public class Constant {

    /**
     * 常量类私有无参构造
     */
    private Constant(){}

    /**
     * 验证码---默认过期时间  单位秒
     */
    public static final long CODE_DEFAULT_EXPIRE_TIME_GRAPH = 60 * 5;

    /**
     * 验证码长度
     */
    public static final int CODE_LENGTH = 6;

    /**
     * 日期格式化
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 时间格式化
     */
    public static final String TIME_FORMAT = "HH:mm:ss";

    /**
     * 时间格式化
     */
    public static final String DATE_TIME_FORMAT = DATE_FORMAT+" "+TIME_FORMAT;

    /**
     * 对象名称分隔符
     */
    public static final String SEPARATOR_BEAN_NAME = "#";

    /**
     * 时区
     */
    public static final String TIME_ZONE = "GMT+8";

    /**
     * 时间格式化对象
     */
    public static final DateTimeFormatter FORMATTER = DateTimeFormatter
            .ofPattern(Constant.DATE_TIME_FORMAT)
            .withZone(ZoneId.of(Constant.TIME_ZONE));

    /**
     * 默认分页当前页码
     */
    public static final int DEFAULT_PAGE_CURRENT = 1;

    /**
     * 默认分页每页显示数量
     */
    public static final int DEFAULT_PAGE_SIZE = 10;

    /**
     * 默认分页排序方式
     */
    public static final String DEFAULT_PAGE_ORDER_TYPE = OrderTypeEnum.DESC.getCode();

    /**
     * 默认分页排序字段
     */
    public static final String DEFAULT_PAGE_ORDER_FIELD = "created_time";

    /**
     * 文件服务器 nginx拦截前缀
     */
    public static final String MINIO = "minio";

    /**
     * 序列化对象
     */
    public static final Gson GSON = new GsonBuilder()
            .setDateFormat(DATE_TIME_FORMAT)
            .registerTypeAdapter(LocalDateTime.class,
                    (JsonSerializer<LocalDateTime>) (localDateTime, type, jsonSerializationContext) ->
                            new JsonPrimitive(localDateTime.format(FORMATTER)))
            .registerTypeAdapter(LocalDateTime.class,
                    (JsonDeserializer<LocalDateTime>) (jsonElement, type, jsonDeserializationContext) -> {
                        String datetime = jsonElement.getAsJsonPrimitive().getAsString();
                        return LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
                    })
            .registerTypeAdapter(LocalDate.class,
                    (JsonSerializer<LocalDate>) (localDate, type, jsonSerializationContext) ->
                            new JsonPrimitive(localDate.format(FORMATTER)))
            .registerTypeAdapter(LocalDateTime.class,
                    (JsonDeserializer<LocalDate>) (jsonElement, type, jsonDeserializationContext) -> {
                        String datetime = jsonElement.getAsJsonPrimitive().getAsString();
                        return LocalDate.parse(datetime, DateTimeFormatter.ofPattern(DATE_FORMAT));
                    })
            .registerTypeAdapter(Double.class, (JsonSerializer<Double>) (jsonElement, type, jsonDeserializationContext)-> {
                if (jsonElement == jsonElement.longValue()) {
                    return new JsonPrimitive(jsonElement.longValue());
                }
                return new JsonPrimitive(jsonElement);
            })
            .create();
}
